//
//  MyOrderViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/28.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "MyOrderViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "ZYButton.h"
#import "Order.h"
#import "ZYTimer.h"
#import "ZYTableViewCell.h"
#import "TimeTableViewCell.h"
#import "OYCountDownManager.h"
#import "CenterWebViewController.h"
#import "OrderEvaluationViewController.h"
#import "EaseMessageViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface MyOrderViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,retain)UITableView *mainTableView;
@property (nonatomic,retain)UIButton *selectClassBtn;
@property (nonatomic,retain)UIView *lineView;
@property (nonatomic,retain)NSMutableArray *classArr;
@property (nonatomic,retain)NSMutableArray *dataArr;
@property (nonatomic,retain)NSString *orderClass;
@property (nonatomic,retain)ZYTimer *timer;
@property (nonatomic,retain)NSMutableArray *timeArr;
@property (nonatomic,copy)UIView *menuView;
@property (nonatomic,copy)ZYButton *inAction;
@property (nonatomic,copy)ZYButton *endAction;
@property (nonatomic,copy)ZYButton *flowAction;
@property (assign, nonatomic) BOOL pageToEnd;
@property (assign, nonatomic) NSInteger page;
@property (assign, nonatomic) NSInteger limit;
@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.limit = 5;
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100*kScale, kScreenW, kScreenH-100*kScale-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
    [self addRefreshHeader:_mainTableView];
    [self addLoadMoreFooter:_mainTableView];
    _dataArr = [NSMutableArray array];
    _orderClass = @"0";
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    [self request];
    [self setUI];
}
- (void) setUI
{
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"我的订单"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
//    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
//    buttonNotice.frame = CGRectMake(0, 0, 20, 20);
//    [buttonNotice setBackgroundImage:[UIImage imageNamed:@"筛选图标"] forState:UIControlStateNormal];
//    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
//    [buttonNotice addTarget:self action:@selector(menuClick:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = btnNoticeItem;
//
    
    UIScrollView *classScrollView = [[UIScrollView alloc]init];
    classScrollView.delegate = self;
    //        hotTribeScrollView.backgroundColor = [UIColor redColor];
    classScrollView.frame = CGRectMake(0, 0, kScreenW,100*kScale);
    //    CGFloat count = 20;
    //    hotTribeScrollView.contentSize = CGSizeMake((220*kScale+10)*count, 0);
    //设置分页
    //        hotTribeScrollView.pagingEnabled = YES;
    // 是否同时运动,lock
    classScrollView.directionalLockEnabled = YES;
    //隐藏滑动条
    classScrollView.showsHorizontalScrollIndicator = FALSE;
    classScrollView.showsVerticalScrollIndicator = FALSE;
    classScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:classScrollView];
    _classArr = [NSMutableArray arrayWithObjects:@"全部订单",@"待付款",@"待发货",@"待收货",@"待评价",@"已关闭",@"已完成", nil];
    //设置 content size
    float buttonWidth = 0.f;
    
    for (NSUInteger i = 0; i<_classArr.count; i++) {
        
        ZYButton * button = [ZYButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:_classArr[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        CGFloat width = [_classArr[i] boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        //为button赋值
        //        [button setTitle:arr[i] forState:UIControlStateNormal];
        
        //        CGSize titleSize = [YLSlideTitleView boudingRectWithSize:CGSizeMake(SCREEN_WIDTH_YLSLIDE, YLSildeTitleViewHeight)
        //                                                           label:button.titleLabel];
        
        CGRect frame;
        frame.origin = CGPointMake(buttonWidth+30*kScale, 0);
        frame.size   = CGSizeMake(width+20*kScale,100*kScale);
        [button setFrame:frame];
        
        buttonWidth += CGRectGetWidth(button.frame);
        
        button.tag = i;
        button.backgroundColor = [UIColor clearColor];
        
        [button addTarget:self
                   action:@selector(buttonEvents:)
         forControlEvents:UIControlEventTouchUpInside];
        
        //        [self configButtonWithOffsetx:0];
        if (i==0)
        {
            _selectClassBtn = button;
        }
        [classScrollView addSubview:button];
    }
    classScrollView.contentSize = CGSizeMake(buttonWidth+60*kScale, 100*kScale);
    _lineView = [[UIView alloc]init];
    _lineView.frame = CGRectMake(_selectClassBtn.frame.origin.x, 97*kScale, _selectClassBtn.frame.size.width, 3*kScale);
    _lineView.backgroundColor = [UIColor colorWithRed:14/255.0 green:161/255.0 blue:182/255.0 alpha:1.0f];
    [classScrollView addSubview:_lineView];
    
    
    
    
    // 启动倒计时管理
    [kCountDownManager start];
 
}
//下拉刷新
- (void)refresh {
    [super refresh];
    [self request];
}
//上拉加载
- (void)loadMore {
    if (self.pageToEnd) {
        [ViewFactory showInfoMsg:@"没有下一页了！"];
        [_mainTableView.footer endRefreshing];
        return;
    }
    
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *urlStr = [[NSString stringWithFormat:@"%@/userCenter/selectMyOrdersByUserId?status=%@&page=%ld&limit=%ld",APIYIGOU,[NSString stringWithFormat:@"%@",_orderClass],self.page,self.limit]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    DYLog(@".....%ld",self.page);
    request.requestUrl = urlStr;
    [ViewFactory showLoading];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSArray *array = allDic[@"data"][@"list"];
        DYLog(@"66666%@",array);
        if (array.count == 0) {
            [ViewFactory showInfoMsg:@"没有下一页了！"];
            
        }
        else if (array.count<_limit)
        {
            self.pageToEnd = YES;
        }
        self.page += 1;
        DYLog(@".....%ld",array.count);
        [_dataArr addObjectsFromArray: array];
        DYLog(@".....%ld",_dataArr.count);
        [ViewFactory dismissLoading];
        [_mainTableView reloadData];
        [_mainTableView.footer endRefreshing];
    } error:^(YTKBaseRequest *request) {
        [_mainTableView.footer endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        [_mainTableView.footer endRefreshing];
    }];
}

- (void)request
{
    self.page = 1;
    self.pageToEnd = NO;
    NSString *urlStr = [[NSString stringWithFormat:@"%@/userCenter/selectMyOrdersByUserId?status=%@&page=%ld&limit=%ld",APIYIGOU,[NSString stringWithFormat:@"%@",_orderClass],self.page,self.limit]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        [_dataArr removeAllObjects];
        if([allDic[@"data"] isKindOfClass:[NSDictionary class
                                           ]]){
            [_dataArr addObjectsFromArray: allDic[@"data"][@"list"]];
            // 调用[kCountDownManager reload]
            
            [_mainTableView reloadData];
            
            [kCountDownManager reload];
        }
        self.page+=1;
        [_mainTableView.header endRefreshing];
        DYLog(@"allDic====%@",allDic);
        DYLog(@"dataArr====%ld",_dataArr.count);

    } error:^(YTKBaseRequest *request) {
        [_mainTableView.header endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [_mainTableView.header endRefreshing];
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}
- (void)refreshLessTime
{
//    NSUInteger time;
//    for (int i = 0; i < _dataArr.count; i++) {
//        Order *order = [[Order alloc]initWithDic:_dataArr[i]];
//        
//        time = [[[totalLastTime objectAtIndex:i] objectForKey:@"lastTime"]integerValue];
//        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:[[[totalLastTime objectAtIndex:i] objectForKey:@"indexPath"] integerValue]];
//        ZYTableViewCell *cell = (ZYTableViewCell *)[_mainTableView cellForRowAtIndexPath:indexPath];
//        cell.timeLable.text = [NSString stringWithFormat:@"剩余支付时间：%@",[self lessSecondToDay:--time]];
//        NSDictionary *dic = @{@"indexPath": [NSStringstringWithFormat:@"%i",indexPath.section],@"lastTime": [NSStringstringWithFormat:@"%i",time]};
//        [totalLastTime replaceObjectAtIndex:i withObject:dic];
//    }
}

- (NSString *)lessSecondToDay:(NSUInteger)seconds
{
    NSUInteger day  = (NSUInteger)seconds/(24*3600);
    NSUInteger hour = (NSUInteger)(seconds%(24*3600))/3600;
    NSUInteger min  = (NSUInteger)(seconds%(3600))/60;
    NSUInteger second = (NSUInteger)(seconds%60);
    
    NSString *time = [NSString stringWithFormat:@"%lu日%lu小时%lu分钟%lu秒",(unsigned long)day,(unsigned long)hour,(unsigned long)min,(unsigned long)second];
    return time;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    ZYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
    }
    //设置cell点击背景色不变
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    Order *order = [[Order alloc]initWithDic:_dataArr[indexPath.section]];
    DYLog(@"orderid==%@",order.orderId);
    if (indexPath.row==0) {
        UIImageView *buyerHeader = [[UIImageView alloc]init];
        [buyerHeader sd_setImageWithURL:[NSURL URLWithString:order.sellerInfo.headerImageUrl]];
        buyerHeader.frame = CGRectMake(30*kScale, 30*kScale, 42*kScale, 42*kScale);
        buyerHeader.layer.masksToBounds = YES;
        buyerHeader.layer.cornerRadius = 21*kScale/2;
        [cell addSubview:buyerHeader];
        UILabel *buyerNameLable = [[UILabel alloc]init];
        buyerNameLable.frame = CGRectMake(90*kScale, 30*kScale, kScreenW, 42*kScale);
        buyerNameLable.text = order.sellerInfo.name;
        buyerNameLable.font = [UIFont systemFontOfSize:15];
        [cell addSubview:buyerNameLable];
        if ([order.orderStatus isEqualToString:@"1"])
        {
            NSDateFormatter  *dateFormatter = [[NSDateFormatter alloc]init];
            //        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *date = [dateFormatter dateFromString:order.createTime];
//            NSString *createDateStr = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
            NSDate *nowDate = [NSDate date];
            NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
            
            NSTimeInterval timeWrong = [zone secondsFromGMTForDate:nowDate];// 以秒为单位返回当前时间与系统格林尼治时间的差
            
            NSDate *dateNow = [nowDate dateByAddingTimeInterval:timeWrong];
//            NSString *nowDateStr = [NSString stringWithFormat:@"%ld",(long)[nowDate timeIntervalSince1970]];
            NSTimeInterval time = [date timeIntervalSinceDate:dateNow];
            cell.countStr = [NSString stringWithFormat:@"%f",time];
            cell.timeLable.hidden = NO;
        }

        else
        {
            DYLog(@"orderSattus==%@",order.orderStatus);
            cell.timeLable.hidden = YES;
            UILabel *timeLable2 = [[UILabel alloc]init];
            if ([order.orderStatus isEqualToString:@"2"])
            {
                timeLable2.text = @"已付款，等待卖家发货";
            }
            else if ([order.orderStatus isEqualToString:@"3"])
            {
                timeLable2.text = @"卖家已发货，等待收货";
            }
            else if ([order.orderStatus isEqualToString:@"4"])
            {
                timeLable2.text = @"交易成功，等待评价";
            }
            else if ([order.orderStatus isEqualToString:@"5"])
            {
                timeLable2.text = @"订单已关闭";
            }
            else if ([order.orderStatus isEqualToString:@"6"])
            {
                timeLable2.text = @"订单已完成";
            }
            
            timeLable2.font = [UIFont systemFontOfSize:13];
            CGSize stateSize = [timeLable2 sizeThatFits:CGSizeMake(MAXFLOAT, 42*kScale)];
            timeLable2.frame = CGRectMake(kScreenW-30*kScale-stateSize.width, 30*kScale, stateSize.width, 42*kScale);
            timeLable2.adjustsFontSizeToFitWidth = YES;
            timeLable2.textColor = [UIColor colorWithRed:14/255.0 green:161/255.0 blue:182/255.0 alpha:1.0f];
            timeLable2.textAlignment = NSTextAlignmentRight;
            [cell addSubview:timeLable2];
        }
        cell.timeLable.font = [UIFont systemFontOfSize:13];
        CGSize stateSize = [cell.timeLable sizeThatFits:CGSizeMake(MAXFLOAT, 42*kScale)];
        cell.timeLable.frame = CGRectMake(kScreenW-30*kScale-stateSize.width, 30*kScale, stateSize.width, 42*kScale);
        cell.timeLable.adjustsFontSizeToFitWidth = YES;
        cell.timeLable.textColor = [UIColor colorWithRed:14/255.0 green:161/255.0 blue:182/255.0 alpha:1.0f];
        cell.timeLable.textAlignment = NSTextAlignmentRight;
        [cell addSubview:cell.timeLable];
    }
    else if (indexPath.row==1)
    {
        UIImageView *headerImage = [[UIImageView alloc]init];
        headerImage.frame = CGRectMake(30*kScale, 30*kScale, 150*kScale, 150*kScale);
        //        tribeHeaderImage.backgroundColor = [UIColor cyanColor];
        //    [tribeHeaderImage sd_setImageWithURL:[NSURL URLWithString:@"rest"]];
        NSString *urlStr = [order.goods.headerImageUrl componentsSeparatedByString:@","][0];
        [headerImage sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        headerImage.layer.masksToBounds  = YES;
        headerImage.layer.cornerRadius = 8*kScale;
        [cell addSubview:headerImage];
        
        UILabel *introdocLable = [[UILabel alloc]init];
        introdocLable.font = [UIFont systemFontOfSize:13];
        introdocLable.numberOfLines = 3;
        //        introdocLable.lineBreakMode =  NSLineBreakByTruncatingTail;
        introdocLable.textColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1.0];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
        style.lineSpacing = 5;
        introdocLable.text = order.goodsName;
        CGSize introSize = [introdocLable sizeThatFits:CGSizeMake(kScreenW-(150*kScale+3*30*kScale), MAXFLOAT)];
        
        //        NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:style};
        //        introdocLable.attributedText = [[NSAttributedString alloc]initWithString:introdocLable.text attributes:attributes];
        
        
        introdocLable.frame = CGRectMake(150*kScale+2*30*kScale, headerImage.frame.origin.y, kScreenW-(150*kScale+3*30*kScale), introSize.height);
        
        [cell addSubview:introdocLable];
        
        UILabel *priceLable= [[UILabel alloc]init];
        priceLable.font = [UIFont systemFontOfSize:13];
        priceLable.text = [NSString stringWithFormat:@"¥%@",order.price];
        CGSize numSize = CGSizeZero;
        
        numSize = [priceLable sizeThatFits:CGSizeMake(MAXFLOAT, 40*kScale)];
        priceLable.frame = CGRectMake(150*kScale+2*30*kScale, headerImage.frame.origin.y+150*kScale-40*kScale, numSize.width, 40*kScale);
        
        
        priceLable.textColor = [UIColor colorWithRed:189/255.0 green:40/255.0 blue:40/255.0 alpha:1.0];
        [cell addSubview:priceLable];
        
        UILabel *sellerClassLable= [[UILabel alloc]init];
        sellerClassLable.font = [UIFont systemFontOfSize:11];
        if ([order.goods.sellerType isEqualToString:@"0"]) {
            sellerClassLable.text = @"一口价";
        }
        else if ([order.goods.sellerType isEqualToString:@"1"])
        {
            sellerClassLable.text = @"拍卖商品";
        }
        sellerClassLable.textColor = [UIColor whiteColor];
        sellerClassLable.backgroundColor = [UIColor colorWithRed:14/255.0 green:161/255.0 blue:182/255.0 alpha:1.0f];
        CGSize seallerSize = [sellerClassLable sizeThatFits:CGSizeMake(MAXFLOAT, 40*kScale)];
        sellerClassLable.frame = CGRectMake(kScreenW-30*kScale-seallerSize.width, headerImage.frame.origin.y+150*kScale-40*kScale,seallerSize.width, 40*kScale);
        [cell addSubview:sellerClassLable];
    }
    if (indexPath.row==2)
    {
        UILabel *numLable= [[UILabel alloc]init];
        numLable.font = [UIFont systemFontOfSize:13];
        numLable.text = order.goodsNumber;
        CGSize numSize = CGSizeZero;
        
        numSize = [numLable sizeThatFits:CGSizeMake(MAXFLOAT, 40*kScale)];
        numLable.frame = CGRectMake(210*kScale, 30*kScale, numSize.width, 40*kScale);
        //        numLable.textColor = [UIColor colorWithRed:189/255.0 green:40/255.0 blue:40/255.0 alpha:1.0];
        [cell addSubview:numLable];
        UILabel *allPriceLable= [[UILabel alloc]init];
        allPriceLable.font = [UIFont systemFontOfSize:13];
        allPriceLable.text = [NSString stringWithFormat:@"合计：¥%0.2f(含运费¥%0.2f)",[order.allPrice floatValue],[order.freightMoney floatValue]];
        //        allPriceLable.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1.0];
        CGSize allPriceSize = [allPriceLable sizeThatFits:CGSizeMake(MAXFLOAT, 40*kScale)];
        allPriceLable.frame = CGRectMake(210*kScale+numSize.width+30*kScale, 30*kScale,allPriceSize.width, 42*kScale);
        [cell addSubview:allPriceLable];
        ZYButton *contactBtn = [[ZYButton alloc]initWithFrame:CGRectMake(kScreenW-30*kScale-178*kScale, 82*kScale, 178*kScale,60*kScale)];
        contactBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        contactBtn.layer.cornerRadius  = 8*kScale;
        contactBtn.layer.masksToBounds = YES;
        contactBtn.layer.borderWidth = 1.0f;
        contactBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
//        [contactBtn setTitle:@"联系买家" forState:UIControlStateNormal];
        [contactBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        contactBtn.order = order;
        [cell addSubview:contactBtn];
        
        ZYButton *stateBtn = [[ZYButton alloc]initWithFrame:CGRectMake(kScreenW-30*kScale-178*2*kScale-30*kScale, 82*kScale, 178*kScale,60*kScale)];
        stateBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        stateBtn.layer.cornerRadius  = 8*kScale;
        stateBtn.layer.masksToBounds = YES;
        stateBtn.layer.borderWidth = 1.0f;
        stateBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
//                [stateBtn setTitle:@"查看物流" forState:UIControlStateNormal];
        [stateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        stateBtn.order = order;
        [cell addSubview:stateBtn];
        if ([order.orderStatus isEqualToString:@"1"])
        {
            [stateBtn setTitle:@"取消订单" forState:UIControlStateNormal];
//            stateBtn.titleLabel.text = @"取消订单";
            [stateBtn addTarget:self action:@selector(cancleOrderClick:) forControlEvents:UIControlEventTouchUpInside];
            [contactBtn setTitle:@"联系卖家" forState:UIControlStateNormal];
//            contactBtn.titleLabel.text = @"联系卖家";
            [contactBtn addTarget:self action:@selector(relationSellerClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        else if ([order.orderStatus isEqualToString:@"2"])
        {
//            timeLable2.text = @"卖家已发货，等待收货";
//            stateBtn.titleLabel.text = @"取消订单";
//            [stateBtn addTarget:self action:@selector(cancleOrderClick:) forControlEvents:UIControlEventTouchUpInside];
            stateBtn.hidden = YES;
            [contactBtn setTitle:@"提醒卖家" forState:UIControlStateNormal];
//            contactBtn.titleLabel.text = @"提醒卖家";
            [contactBtn addTarget:self action:@selector(relationSellerClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        else if ([order.orderStatus isEqualToString:@"3"])
        {
            [stateBtn setTitle:@"查看物流" forState:UIControlStateNormal];
//            stateBtn.titleLabel.text = @"查看物流";
            [stateBtn addTarget:self action:@selector(checkLogisticsClick:) forControlEvents:UIControlEventTouchUpInside];
            [contactBtn setTitle:@"联系卖家" forState:UIControlStateNormal];
//            contactBtn.titleLabel.text = @"联系卖家";
            [contactBtn addTarget:self action:@selector(relationSellerClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        else if ([order.orderStatus isEqualToString:@"4"])
        {
            [stateBtn setTitle:@"评价订单" forState:UIControlStateNormal];
//            stateBtn.titleLabel.text = @"评价订单";
            [stateBtn addTarget:self action:@selector(evaluateOrderClick:) forControlEvents:UIControlEventTouchUpInside];
            [contactBtn setTitle:@"联系卖家" forState:UIControlStateNormal];
//            contactBtn.titleLabel.text = @"联系卖家";
            [contactBtn addTarget:self action:@selector(relationSellerClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        else if ([order.orderStatus isEqualToString:@"5"])
        {
            stateBtn.hidden = YES;
            [contactBtn setTitle:@"联系卖家" forState:UIControlStateNormal];
//            contactBtn.titleLabel.text = @"联系卖家";
            [contactBtn addTarget:self action:@selector(relationSellerClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            stateBtn.hidden = YES;
            [contactBtn setTitle:@"联系卖家" forState:UIControlStateNormal];
            //            contactBtn.titleLabel.text = @"联系卖家";
            [contactBtn addTarget:self action:@selector(relationSellerClick:) forControlEvents:UIControlEventTouchUpInside];
        }

    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return 100*kScale;
    }
    else if (indexPath.row==1)
    {
        return 210*kScale;
    }
    return 164*kScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0)
    {
        return 30*kScale;
    }
    return 20*kScale;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
//        @"%@/hyContent/addReport?report_obj=%@&report_obj_id=%@&report_type=%@&report_content=%@", APIYIGOU, @"01", self.articleContent, @"00", @""];
        Order *order = [[Order alloc]initWithDic:_dataArr[indexPath.section]];
        NSString *urlStr = [NSString stringWithFormat:@"%@/h5/mobile/#/order_detail/%@/%@",APIYIGOU,order.orderId,[UserData usertoken]];
        CenterWebViewController *myWeb = [[CenterWebViewController alloc]init];
        myWeb.url = urlStr;
        myWeb.titleStr = @"订单详情";
        DYLog(@"0000000%@",urlStr);
        [self.navigationController pushViewController:myWeb animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (void)buttonEvents:(ZYButton*)sender
{
    _selectClassBtn = sender;
    _orderClass = [NSString stringWithFormat:@"%ld",sender.tag];
    _lineView.frame = CGRectMake(_selectClassBtn.frame.origin.x, 97*kScale, _selectClassBtn.frame.size.width, 3*kScale);
    [self request];
}


//返回
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)stateClick:(ZYButton*)sender
{
//    _menuView.frame = CGRectMake(kScreenW*2, 0, 0, 0);
//    _stateStr = [NSString stringWithFormat:@"%ld",(long)sender.tag];
//    [self request];
}
//取消订单
- (void)cancleOrderClick:(ZYButton*)sender
{
    NSString *urlStr = [[NSString stringWithFormat:@"%@/order/cancelOrder?order_id=%@",APIYIGOU,sender.order.orderId]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"1111111%@",allDic);
        [self request];
        [_mainTableView reloadData];
        [ViewFactory showSuccess:@"取消订单成功"];
    } error:^(YTKBaseRequest *request) {
        DYLog(@"%@",request);
        [ViewFactory showInfoMsg:@"取消订单失败"];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}
//联系卖家
- (void)relationSellerClick:(ZYButton*)sender
{
//    OrderEvaluationViewController *vc = [[OrderEvaluationViewController alloc]init];
//    vc.order = sender.order;
//    [self.navigationController pushViewController:vc animated:YES];
    EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:sender.order.sellerInfo.userId conversationType:EMConversationTypeChat];
    DYLog(@"userId==%@",sender.order.sellerInfo.userId);
    [self.navigationController pushViewController:chatController animated:YES];
}
//查看物流
- (void)checkLogisticsClick:(ZYButton*)sender
{
    
    Order *order = sender.order;
    NSString *urlStr = [NSString stringWithFormat:@"%@/h5/mobile/#/logistics_status/%@/%@",APIYIGOU,order.orderId,[UserData usertoken]];
    CenterWebViewController *myWeb = [[CenterWebViewController alloc]init];
    myWeb.url = urlStr;
    myWeb.titleStr = @"物流信息";
    DYLog(@"0000000%@",urlStr);
    [self.navigationController pushViewController:myWeb animated:YES];
}
//评价订单
- (void)evaluateOrderClick:(ZYButton*)sender
{
    OrderEvaluationViewController *vc = [[OrderEvaluationViewController alloc]init];
    vc.order = sender.order;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
